package address

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	USER "OnlineRetailers/models/User"
	"OnlineRetailers/models/User/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetAddress struct {
	beego.Controller
}

func (c *GetAddress) GetAddress() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Address/GetAddress"
	res := resMes.GetAddress{}
	address := []*USER.Address{}
	log := &error2.Log{}
	var err error
	o := orm.NewOrm()

	err, openid := login.CheckOpenid(c.Ctx.Request.Header["Openid"])
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	fmt.Println(openid)
	sql := "select * from `address` where user_id=?"
	_, err = o.Raw(sql, openid).QueryRows(&address)
	//fmt.Println(address[0])

	//_, err = o.QueryTable("Activity").All(&activitys)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	res.Address = address
	res.Code = "200"
	c.Data["json"] = res

}
